﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/6/6 星期四 23:37:46
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;
using Sector7.Medical.DAO.Utility;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 科室数据访问对象
    /// </summary>
    public class DepartmentDAO : DAOBase<M7_Department>, IDepartmentDAO
    {
        #region Public functions

        /// <summary>
        /// 根据科室标志获取所有科室信息
        /// </summary>
        /// <param name="flags">科室标志</param>
        /// <returns></returns>
        public List<M7_Department> ReadAll(int flags)
        {
            string sql = @"
                SELECT *
                    FROM M7_Department
                    WHERE Flags & @Flag = @Flags";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_Department> departmentList = new List<M7_Department>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(flags, "Flag"),
                    DBUtil.CreateSqlParameter(flags, "Flags"));
                while (reader.Read())
                {
                    object medicinalType = new M7_Department();
                    DBUtil.ReadData(reader, medicinalType);

                    departmentList.Add(medicinalType as M7_Department);
                }

                return departmentList;
            }
        }

        /// <summary>
        /// 根据科室标志获取所有科室信息
        /// </summary>
        /// <param name="comflags">科室标志</param>
        /// <returns></returns>
        public List<M7_Department> ReadAllByComflags(int comflags)
        {
            string sql = @"
                SELECT *
                    FROM M7_Department
                    WHERE @Flags & Flags = Flags";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_Department> departmentList = new List<M7_Department>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(comflags, "Flags"));
                while (reader.Read())
                {
                    object medicinalType = new M7_Department();
                    DBUtil.ReadData(reader, medicinalType);

                    departmentList.Add(medicinalType as M7_Department);
                }

                return departmentList;
            }
        }

        /// <summary>
        /// 添加科室
        /// </summary>
        /// <param name="department">科室信息</param>
        /// <returns></returns>
        public override object Create(M7_Department department)
        {
            string sql = @"
                INSERT INTO M7_Department
                        (DeptName
                        ,SpellCode
                        ,ParentId
                        ,AreaId
                        ,IsValid
                        ,Remarks
                        ,Flags)
                    VALUES
                        (@DeptName
                        ,@SpellCode
                        ,@ParentId
                        ,@AreaId
                        ,@IsValid
                        ,@Remarks
                        ,@Flags)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(department.DeptId, "DepttId"),
                    DBUtil.CreateSqlParameter(department.DeptName, "DeptName"),
                    DBUtil.CreateSqlParameter(department.SpellCode, "SpellCode"),
                    DBUtil.CreateSqlParameter(department.ParentId, "ParentId"),
                    DBUtil.CreateSqlParameter(department.AreaId, "AreaId"),
                    DBUtil.CreateSqlParameter(department.IsValid, "IsValid"),
                    DBUtil.CreateSqlParameter(department.Remarks, "Remarks"),
                    DBUtil.CreateSqlParameter(department.Flags, "Flags"));
            }
        }

        /// <summary>
        /// 编辑二级科室Flags
        /// </summary>
        /// <param name="parentDeptId">一级科室ID</param>
        /// <param name="flags">标志</param>
        /// <returns></returns>
        public int UpdateSubDepartmentFlags(int parentDeptId, int flags)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                UPDATE M7_Department
                    SET Flags = @Flags
                    WHERE 1 = 1
                    AND ParentId = @ParentId";

                return opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(flags, "Flags"),
                    DBUtil.CreateSqlParameter(parentDeptId, "ParentId"));
            }
        }
        #endregion

        #region Structure
        public DepartmentDAO()
        {
            m_tableName = "M7_Department";
            m_primaryKeyName = "DeptId";
        }
        #endregion
    }
}
